Imports System.Windows.Controls
Imports System.Windows.Input
Imports System.Windows

Public Class SimpleListBox
    Inherits ListBox

    Sub New()
        Me.ItemContainerStyle = ModuleDashboarding._propertyGridDictionary("FilterListBoxItemStyle")
        Me.ItemTemplate = ModuleDashboarding._propertyGridDictionary("FilterListBoxItemTemplate")
        Me.SelectionMode = Controls.SelectionMode.Multiple
        'DisplayMemberPath = "Member"
        'ContextMenu = New ContextMenu()

        ''Dim mi = New MenuItem() With {.Header = "Tout Selectionner"}
        ''AddHandler mi.Click, AddressOf ToutSelectionnerClick
        ''ContextMenu.Items.Add(mi)

        'Dim mi = New MenuItem() With {.Header = "Tout Deselectionner"}
        'AddHandler mi.Click, AddressOf ToutDeselectionnerClick
        'ContextMenu.Items.Add(mi)

        'mi = New MenuItem() With {.Header = "Inverser la Selection"}
        'AddHandler mi.Click, AddressOf InverserlaSelectionClick
        'ContextMenu.Items.Add(mi)
    End Sub

    Protected Overrides Function ShouldSerializeProperty(dp As DependencyProperty) As Boolean
        If dp.Name = "ItemTemplate" Then Return False

        If dp.Name = "ItemContainerStyle" Then Return False

        If dp.Name = "ItemsSource" Then Return False

        If dp.Name = "Items" Then Return False

        Return MyBase.ShouldSerializeProperty(dp)
    End Function

    Private Sub ToutSelectionnerClick(sender As Object, e As RoutedEventArgs)
        'Dim data = TryCast(ItemsSource, DataView)
        'If data IsNot Nothing Then
        '    For i = 0 To data.Count - 1
        '        data(i)("IsSelected") = 1.0
        '    Next
        'End If
        Me.SelectedItems.Clear()
    End Sub

    Private Sub ToutDeselectionnerClick(sender As Object, e As RoutedEventArgs)
        Dim data = TryCast(ItemsSource, DataView)
        If data IsNot Nothing Then
            For i = 0 To data.Count - 1
                data(i)("IsSelected") = 0.0
            Next
        End If
    End Sub

    Private Sub InverserlaSelectionClick(sender As Object, e As RoutedEventArgs)
        Dim data = TryCast(ItemsSource, DataView)
        If data IsNot Nothing Then
            For i = 0 To data.Count - 1
                If IsDBNull(data(i)("IsSelected")) Then
                    data(i)("IsSelected") = 1.0
                ElseIf data(i)("IsSelected") = 0.0 Then
                    data(i)("IsSelected") = 1.0
                Else
                    data(i)("IsSelected") = 0.0
                End If
            Next
        End If
    End Sub




    'Public Property MouseOverStyle As Style
    '    Get
    '        Return GetValue(MouseOverStyleProperty)
    '    End Get

    '    Set(ByVal value As Style)
    '        SetValue(MouseOverStyleProperty, value)
    '    End Set
    'End Property

    'Public Shared ReadOnly MouseOverStyleProperty As DependencyProperty = _
    '                       DependencyProperty.Register("MouseOverStyle", _
    '                       GetType(Style), GetType(SimpleListBox), _
    '                       New PropertyMetadata(Nothing))



    'Public Property SelectedStyle As Style
    '    Get
    '        Return GetValue(SelectedStyleProperty)
    '    End Get

    '    Set(ByVal value As Style)
    '        SetValue(SelectedStyleProperty, value)
    '    End Set
    'End Property

    'Public Shared ReadOnly SelectedStyleProperty As DependencyProperty = _
    '                       DependencyProperty.Register("SelectedStyle", _
    '                       GetType(Style), GetType(SimpleListBox), _
    '                       New PropertyMetadata(Nothing))



End Class

Partial Public Class ListboxZone
    Inherits DashboardZone

    Dim _Visual As New SimpleListBox()
    Dim _DataProperty As DashboardZoneProperty
    Dim _Filter As DataSourceFilter
    Dim _SelectedItemContainerStyle As DashboardZoneProperty
    Dim _MouseOverItemContainerStyle As DashboardZoneProperty
    Dim _ItemContainerStyle As DashboardZoneProperty

    Sub New()
        'Me._Visual.Resources.MergedDictionaries.Add(ModuleDashboarding._propertyGridDictionary)
        'Me._Visual.IsSynchronizedWithCurrentItem = True
        'Me._Visual.ItemTemplate = ModuleDashboarding._propertyGridDictionary("FilterListBoxItemTemplate")
        'Me._Visual.SetResourceReference(ListBox.ItemContainerStyleProperty, "FilterListBoxItemStyle")
        Me._Visual.HorizontalContentAlignment = HorizontalAlignment.Stretch
        Me._Visual.Background = Nothing
        Me._Visual.BorderBrush = Nothing
        Me._Visual.BorderThickness = New Thickness(0)
        AddHandler CType(Me._Visual.SelectedItems, Specialized.INotifyCollectionChanged).CollectionChanged, AddressOf VisualSelectedItemsCollectionChanged

        Me._Visual.SelectedValuePath = "Member"
        Me._DataProperty = New DashboardZoneProperty(Me, "Data", GetType(DataView))
        Me._Filter = New DataSourceFilter()
        'Me._Filter._owner = Me._Visual
        ModuleDashboarding._activeproject.FilterList.Add(Me._Filter)
        'Data.BindingOperations.SetBinding(Me._Visual, ListBox.SelectedItemProperty, New Data.Binding("Value") With {.Source = Me._Filter, .Mode = Data.BindingMode.TwoWay})
        Me.Properties.Add(_DataProperty)

        ' Me.Properties.Add(New DashboardZoneProperty(Me, "IsSynchronizedWithCurrentItem"))
        'Me.Properties.Add(New DashboardZoneProperty(Me, "SelectedIndex"))
        'Me.Properties.Add(New DashboardZoneProperty(Me, "SelectedItem"))
        ' Me.Properties.Add(New DashboardZoneProperty(Me, "SelectedItems"))
        'Me.Properties.Add(New DashboardZoneProperty(Me, "SelectionMode"))
        '   Me.Properties.Add(New DashboardZoneProperty(Me, "SelectedValue"))
        '    Me.Properties.Add(New DashboardZoneProperty(Me, "SelectedValuePath"))
        '    Me.Properties.Add(New DashboardZoneProperty(Me, "ItemsSource"))
        '  Me.Properties.Add(New DashboardZoneProperty(Me, "DisplayMemberPath"))
        'Me.Properties.Add(New DashboardZoneProperty(Me, "SelectionMode"))
        'Me.Properties.Add(New DashboardZoneProperty(Me, "ItemContainerStyle"))
        Me._ItemContainerStyle = New DashboardZoneProperty(Me, "ItemContainerStyle", GetType(StyleObject))
        Me._SelectedItemContainerStyle = New DashboardZoneProperty(Me, "SelectedItemContainerStyle", GetType(StyleObject))
        Me._MouseOverItemContainerStyle = New DashboardZoneProperty(Me, "MouseOverItemContainerStyle", GetType(StyleObject))
        Me.Properties.Add(_ItemContainerStyle)
        Me.Properties.Add(_SelectedItemContainerStyle)
        Me.Properties.Add(_MouseOverItemContainerStyle)


        'Me.Properties.Add(New DashboardZoneProperty(Me, "BorderBrush"))
        'Me.Properties.Add(New DashboardZoneProperty(Me, "BorderThickness"))
        'Me.Properties.Add(New DashboardZoneProperty(Me, "Background"))
        'Me.Properties.Add(New DashboardZoneProperty(Me, "Foreground"))
        'Me.Properties.Add(New DashboardZoneProperty(Me, "FontFamily"))
        'Me.Properties.Add(New DashboardZoneProperty(Me, "FontSize"))
        'Me.Properties.Add(New DashboardZoneProperty(Me, "FontStretch"))
        'Me.Properties.Add(New DashboardZoneProperty(Me, "FontStyle"))
        'Me.Properties.Add(New DashboardZoneProperty(Me, "FontWeight"))
        'Me.Properties.Add(New DashboardZoneProperty(Me, "HorizontalContentAlignment"))
        'Me.Properties.Add(New DashboardZoneProperty(Me, "VerticalContentAlignment"))
        Me.Properties.Add(New DashboardZoneProperty(Me, "Padding"))
        'Me.Properties.Add(New DashboardZoneProperty(Me, "Margin"))
        'Me.Properties.Add(New DashboardZoneProperty(Me, "HorizontalAlignment"))
        'Me.Properties.Add(New DashboardZoneProperty(Me, "VerticalAlignment"))


    End Sub

    Public Overrides ReadOnly Property DataProperty As DashboardZoneProperty
        Get
            Return _DataProperty
        End Get
    End Property

    Public Overrides ReadOnly Property Visual As UIElement
        Get
            Return _Visual
        End Get
    End Property

    Protected Friend Overrides Sub OnValueChanged([Property] As DashboardZoneProperty)
        If [Property] Is _DataProperty Then
            Dim v = TryCast(_DataProperty.Value, DataView)
            If v IsNot Nothing Then
                If _DataProperty.DataBinding IsNot Nothing Then
                    Me._Filter.DataSource = [Property].DataBinding.DataSource
                    Me._Filter.DataMember = [Property].DataBinding.DataMember
                    'Me._Filter._data = v
                    Me._Filter.Value = Nothing
                    Me._Visual.ItemsSource = v
                End If
            End If
        End If

        If [Property] Is Me._ItemContainerStyle _
            Or [Property] Is Me._SelectedItemContainerStyle _
             Or [Property] Is Me._MouseOverItemContainerStyle Then

            '  Dim originalsetters = CType(ModuleDashboarding._propertyGridDictionary("FilterListBoxItemStyle"), Style).Setters
            Dim s As Style
            If Me._ItemContainerStyle.Value IsNot Nothing Then
                s = StyleObject.ToStyle(TryCast(Me._ItemContainerStyle.Value, StyleObject), GetType(ListBoxItem))
                s.TargetType = GetType(ListBoxItem)
                s.BasedOn = ModuleDashboarding._propertyGridDictionary("FilterListBoxItemStyle")
            Else
                s = New Style()
                s.TargetType = GetType(ListBoxItem)
                s.BasedOn = ModuleDashboarding._propertyGridDictionary("FilterListBoxItemStyle")
            End If
           

            If Me._SelectedItemContainerStyle.Value IsNot Nothing Then
                Dim ss = StyleObject.ToStyle(TryCast(Me._SelectedItemContainerStyle.Value, StyleObject), GetType(ListBoxItem))
                If ss.Setters.Count > 0 Then
                    Dim st = New Trigger() With {.Property = ListBoxItem.IsSelectedProperty, .Value = True}
                    For Each setter As Setter In ss.Setters
                        st.Setters.Add(New Setter(setter.Property, setter.Value))
                    Next
                    s.Triggers.Add(st)
                End If
            End If

            If Me._MouseOverItemContainerStyle.Value IsNot Nothing Then
                Dim ss = StyleObject.ToStyle(TryCast(Me._MouseOverItemContainerStyle.Value, StyleObject), GetType(ListBoxItem))
                If ss.Setters.Count > 0 Then
                    Dim st = New Trigger() With {.Property = ListBoxItem.IsMouseOverProperty, .Value = True}
                    For Each setter As Setter In ss.Setters
                        st.Setters.Add(New Setter(setter.Property, setter.Value))
                    Next
                    s.Triggers.Add(st)
                End If
            End If

            Me._Visual.ItemContainerStyle = s
        End If
    End Sub


    Protected Friend Overrides Sub OnDeserializeVisual(ByVal visual As System.Windows.UIElement)
        MyBase.OnDeserializeVisual(visual)
        Data.BindingOperations.ClearAllBindings(Me._Visual)
        RemoveHandler CType(Me._Visual.SelectedItems, Specialized.INotifyCollectionChanged).CollectionChanged, AddressOf VisualSelectedItemsCollectionChanged
        Me._Visual = Nothing
        Me._Visual = visual
        Me._Visual.ItemContainerStyle = ModuleDashboarding._propertyGridDictionary("FilterListBoxItemStyle")
        Me._Visual.ItemTemplate = ModuleDashboarding._propertyGridDictionary("FilterListBoxItemTemplate")
        AddHandler CType(Me._Visual.SelectedItems, Specialized.INotifyCollectionChanged).CollectionChanged, AddressOf VisualSelectedItemsCollectionChanged
        'Data.BindingOperations.SetBinding(Me._Visual, ListBox.SelectedItemProperty, New Data.Binding("Value") With {.Source = Me._Filter, .Mode = Data.BindingMode.TwoWay})
    End Sub

    Private Sub VisualSelectedItemsCollectionChanged(sender As Object, e As Specialized.NotifyCollectionChangedEventArgs)
        Me._Filter.Value = Me._Visual.SelectedItems
    End Sub

End Class
